package com.travelbid.admin.service;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.travelbid.admin.dao.TUserDAO;
import com.travelbid.admin.pojo.TUser;
import com.travelbid.common.HibernateSessionFactory;

public class AdminUserService {

	private TUserDAO daoUser;

	public void delete(TUser tn) {
		daoUser.delete(tn);
	}

	public TUser findByUserId(Integer id) {
		return daoUser.findById(id);
	}

	public void update(TUser tn) {
		daoUser.save(tn);
	}

	public void saveuser(TUser transientInstance) {
		// log.debug("saving TNews instance");
		Session session = (Session) com.travelbid.common.HibernateSessionFactory
				.getSession();
		try {
			// getSession().save(transientInstance);
			Transaction ts = session.beginTransaction();
			session.saveOrUpdate(transientInstance);
			ts.commit();
			// log.debug("save successful");
		} catch (RuntimeException er) {
			er.printStackTrace();
		} finally {
			session.close();
		}
		// return ;
	}

	public boolean AddNewUser(TUser user) {
		try {
			daoUser.save(user);
			return true;
		} catch (HibernateException e) {
			e.printStackTrace();
		}
		return false;
	}

	@SuppressWarnings("unchecked")
	public int getUserCountByType(Integer type) {
		int num = 0;
		List userList = new ArrayList(0);
		Session session = HibernateSessionFactory.getSession();
		try {
			Transaction transaction = session.beginTransaction();
			Query query = null;
			if (type.intValue() == -1)
				query = session.createQuery("from TUser u");
			else {
				query = session.createQuery("from TUser u where "
						+ "u.type=:type");
				query.setInteger("type", type.intValue());
			}
			userList = query.list();
			transaction.commit();
			return userList.size();
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return num;
	}

	public List getUserListByType(Integer type, int start, int size) {
		List userList = new ArrayList(0);
		Session session = HibernateSessionFactory.getSession();
		try {
			Transaction transaction = session.beginTransaction();
			Query query = null;
			if (type.intValue() == -1) {
				query = session
						.createQuery("from TUser u where u.type!=:admintype");
				query.setInteger("admintype", 0);
			} else {
				query = session.createQuery("from TUser u where "
						+ "u.type=:type");
				query.setInteger("type", type.intValue());
			}
			query.setFirstResult(start);
			query.setMaxResults(size);
			userList = query.list();
			transaction.commit();
			return userList;
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return userList;
	}

	public boolean changUserType(int tId, Integer type) {
		try {
			Session session = HibernateSessionFactory.getSession();
			Transaction transaction = session.beginTransaction();
			TUser olduser = (TUser) session.get(TUser.class, new Integer(tId));
			if (olduser != null) {
				olduser.setType(type);
				session.update(olduser);
				transaction.commit();
				session.close();
				return true;
			} else {
				return false;
			}
		} catch (HibernateException e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean isUserExist(String userName) {
		boolean userExist = false;
		Session session = HibernateSessionFactory.getSession();
		try {
			Query userNameQuery = session.createQuery("from TUser u where "
					+ "u.username=:userName");
			userNameQuery.setString("userName", userName);
			TUser user = (TUser) userNameQuery.uniqueResult();
			if (user != null) {
				userExist = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return userExist;
	}

	public TUser isAdminExist(String userName, String password, int type) {
		TUser user = null;
		Session session = HibernateSessionFactory.getSession();
		try {
			Query query = null;
			if (type == -1) {
				query = session.createQuery("from TUser u where "
						+ "u.username=:userName and u.pwd=:password");
				query.setString("userName", userName);
				query.setString("password", password);
			} else {
				query = session
						.createQuery("from TUser u where "
								+ "u.username=:userName and u.pwd=:password and u.type!=:type");
				query.setString("userName", userName);
				query.setString("password", password);
				query.setInteger("type", type);
			}
			user = (TUser) query.uniqueResult();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return user;
	}

	public TUserDAO getDaoUser() {
		return daoUser;
	}

	public void setDaoUser(TUserDAO daoUser) {
		this.daoUser = daoUser;
	}

	public List findByUsername(String username) {
		Session session = (Session) HibernateSessionFactory.getSession();
		if (username == null || username.trim().equals("")) {
			username = "";
		}
		String hql = "from TUser  where username like '%" + username + "%'";
		try {
			Query query = session.createQuery(hql);
			// query.setString(0, "%"+username+"%");
			List n = (List) query.list();
			return n;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}

	public TUser findById(java.lang.Integer userid) {

		try {
			TUser instance = (TUser) HibernateSessionFactory.getSession().get(
					"com.travelbid.admin.pojo.TUser", userid);
			return instance;
		} catch (RuntimeException re) {

			throw re;
		}
	}

	public TUser checkUserAndPassword(String username, String password) {

		List userList = daoUser.findByUsername(username);
		if (userList.size() > 0) {
			TUser u = (TUser) userList.get(0);
			if (u.getPwd().equals(password)) {
				return u;
			}
		}
		return null;

	}

}
